package net.example.model.contract;
import com.oraycn.omcs.utils.BufferUtils;
import com.oraycn.omcs.utils.SerializeUtils;
import net.example.utils.SerializeHelper;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import io.netty.buffer.ByteBuf;

public class RequestAddFriendContract
{
    public String RequesterID ;
    public String AccepterID ;
    public String RequesterCatalogName ;
    public String Comment ;

    public RequestAddFriendContract(){}
    public RequestAddFriendContract(String requesterID,  String accepterID, String comment,String requesterCatalogName)
    {
        this.RequesterID = requesterID;
        this.AccepterID = accepterID;
        this.Comment = comment;
        this.RequesterCatalogName = requesterCatalogName;
    }

    public byte[] serialize() throws Exception {
        byte[] bAccepterID = AccepterID.getBytes(StandardCharsets.UTF_8);
        byte[] bComment = Comment.getBytes(StandardCharsets.UTF_8);
        byte[] bRequesterCatalogName = RequesterCatalogName.getBytes(StandardCharsets.UTF_8);
        byte[] bRequesterID = RequesterID.getBytes(StandardCharsets.UTF_8);
        int contractLen = 4 + 4 + bAccepterID.length + 4 + bComment.length + 4 + bRequesterCatalogName.length + 4 + bRequesterID.length;
        ByteBuf body = BufferUtils.newBuffer();
        body.writeInt(contractLen);
        body.writeInt(bAccepterID.length);
        body.writeBytes(bAccepterID);
        body.writeInt(bComment.length);
        body.writeBytes(bComment);
        body.writeInt(bRequesterCatalogName.length);
        body.writeBytes(bRequesterCatalogName);
        body.writeInt(bRequesterID.length);
        body.writeBytes(bRequesterID);
        byte[] result = new byte[body.readableBytes()];
        System.arraycopy(body.array(), 0, result, 0, result.length);
        return result;
    }

    public void deserialize(byte[] info) throws IOException
    {
        ByteBuf buffer = SerializeHelper.wrappedBuffer(info);
        buffer.readInt();
        this.AccepterID= SerializeUtils.readStrIntLen(buffer);
        this.Comment=SerializeUtils.readStrIntLen(buffer);
        this.RequesterCatalogName=SerializeUtils.readStrIntLen(buffer);
        this.RequesterID=SerializeUtils.readStrIntLen(buffer);
    }
}

